/*
 * PROJECT: Happy New Year
 * VERSION: 1.0
 * AUTHOR: Mattias Thell
 * URL: http://www.mattiasthell.com
 *
 * */

#include "Audio.hpp"

namespace hny {

	Audio::Audio(FMOD::System* system)
		:mSystem(system), mPlaying(false), mChannel(0), mVolume(1.0f)
	{
	}

	Audio::~Audio()
	{
		mSound->release();
	}

	bool Audio::Load(const std::string& file, bool stream) {
		
		FMOD_RESULT res;
		if(stream)
			res=  mSystem->createStream(file.c_str(), FMOD_DEFAULT, 0, &mSound);
		else
			res = mSystem->createSound(file.c_str(), FMOD_DEFAULT, 0, &mSound);

		if(res != FMOD_OK)
			return false;

		return true;
	}

	void Audio::Play()
	{
		mSystem->playSound(FMOD_CHANNEL_FREE, mSound, true, &mChannel);
		mChannel->setVolume(mVolume);
		mChannel->setPaused(false);
		mPlaying = true;
	}

	void Audio::Pause()
	{
		if(mPlaying) {
			mChannel->setPaused(true);
			mPlaying = false;
		}
	}

	void Audio::Stop()
	{
		if(mPlaying) {
			mChannel->stop();
			mPlaying = false;
		}
	}

	void Audio::SetVolume( float volume )
	{
		mVolume = volume;

		if(mChannel != 0)
			mChannel->setVolume(mVolume);
	}

	float Audio::GetVolume()
	{
		return mVolume;
	}

	bool Audio::IsPlaying()
	{
		if(mChannel == 0)
			return false;

		bool playing;
		mChannel->isPlaying(&playing);
		return playing;
	}

	void Audio::SetLooping( bool loop )
	{
		if(loop)
			mChannel->setMode(FMOD_LOOP_NORMAL);
		else
			mChannel->setMode(FMOD_LOOP_OFF);
	}
}